package com.huafeng.controller;

import com.huafeng.annotations.Log;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.commonutil.JwtHelper;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysUser;
import com.huafeng.model.enums.OperationTypeEnum;
import com.huafeng.model.enums.ResultCodeEnum;
import com.huafeng.model.exception.BusinessException;
import com.huafeng.model.vo.LoginVo;
import com.huafeng.model.vo.SysUserQueryVo;
import com.huafeng.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 花风
 * @date 2023/8/16 18:11
 * @Description SysUserController
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("admin/system/sysUser")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @Log(model = "用户管理",operationType = OperationTypeEnum.OTHER)
    @ApiOperation("用户登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo){
        return sysUserService.login(loginVo);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("获取用户信息")
    @GetMapping("info")
    public Result info(HttpServletRequest request){
        String token = request.getHeader("Token");
        if (StringUtils.isEmpty(token) || StringUtils.isBlank(token)){
            throw new BusinessException(ResultCodeEnum.LOGIN_AUTH);
        }

        // 从token中获取userId
        Long userId = JwtHelper.getUserId(token);
        Assert.isTrue(userId != null && userId > 0,"id必须大于0");
        return sysUserService.info(userId);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("分页条件查询用户信息")
    @GetMapping("page")
    public Result queryUserByPage(SysUserQueryVo sysUserQueryVo){
        Assert.notNull(sysUserQueryVo,"query object must not be null");
        return sysUserService.queryUserByPage(sysUserQueryVo);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("获取用户详情")
    @GetMapping("get/{id}")
    public Result getUserById(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id must not be null and greater than zero");
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.INSERT)
    @ApiOperation("新增用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){

        return sysUserService.saveUser(sysUser);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.UPDATE)
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result update(SysUser sysUser){
        boolean update = sysUserService.updateById(sysUser);
        return CheckUtils.check(update);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.DELETE)
    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id must not be null and greater than zero");
        boolean remove = sysUserService.removeById(id);
        return CheckUtils.check(remove);
    }

    @Log(model = "用户管理",operationType = OperationTypeEnum.STATUS)
    @ApiOperation("更新用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        Assert.isTrue(id != null && id > 0,"id must not be null and greater than zero");
        Assert.isTrue(status != null && status >= 0,"status is illegal");
        return sysUserService.updateStatus(id,status);
    }
}
